﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class entry_course : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string Cno = TextBox1.Text.Trim();
        string Cname = TextBox2.Text.Trim();
        string category = DropDownList1.Text.Trim();
        string priority_no = TextBox3.Text.Trim();
        string credit = TextBox4.Text.Trim();
        string term = TextBox5.Text.Trim();
        string teacher = TextBox6.Text.Trim();
        string address = TextBox7.Text.Trim();
        string time = TextBox8.Text.Trim();
        int temp=0; //统计相同时间，相同地点上课的课程数，即冲突课程数


        string conn = "Data Source= . ;Initial Catalog=XSXK; User ID=sa; pwd=123";
        SqlConnection connection = new SqlConnection(conn);



        try
        {
            if (string.IsNullOrEmpty(Cno) || string.IsNullOrEmpty(Cname) || string.IsNullOrEmpty(category) || string.IsNullOrEmpty(priority_no) || string.IsNullOrEmpty(credit) || string.IsNullOrEmpty(term) || string.IsNullOrEmpty(teacher) || string.IsNullOrEmpty(address) || string.IsNullOrEmpty(time))
            {
                Response.Write("<h1>添加失败，课程的任一项信息不能为空！！</h1>");
            }
            else
            {

                connection.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = connection;

                string str_sql1 = " select count(*) from Course where Course.address='" + address + "' and Course.time='" + time + "' and term='"+ term +"' ";
                comm.CommandText = str_sql1;
                temp = (int)comm.ExecuteScalar();


                if (temp == 0)
                {
                    string str_sql = "insert into Course(Cno , Cname , category , priority_no , credit , term , teacher , address , time) values(  '" + Cno + "' ,  '" + Cname + "', '" + category + "'  ,  '" + priority_no + "'  , '" + credit + "'  ,  '" + term + "'  , '" + teacher + "' , '" + address + "' , '" + time + "' )  ";
                    comm.CommandText = str_sql;
                    //comm.ExecuteScalar();  //如果为查询语句，则用ExecuteScalar()；否则，用ExecuteNonQuery()
                    comm.ExecuteNonQuery();

                    Response.Write("<h1>成功添加课程信息！</h1>");
                }
                else
                    Response.Write(" <h1>课程冲突，不能在同一时间同一地点上多节课！</h1> ");

            }
        }
        catch (SqlException ex)
        {
            Response.Write(ex.Message);
        }

        connection.Close();//关闭数据库连接
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}